package leetcode;

public class LC22 {
    public static void main(String[] args) {
        LC22 lc22 = new LC22();
        ListNode ha = new ListNode(4);
        ListNode hb = new ListNode(5);
        hb.next = new ListNode(6);
        ha.next = hb.next.next = new ListNode(1);
        ha.next.next = hb.next.next.next = new ListNode(8);
        ha.next.next.next= hb.next.next .next.next = new ListNode(4);
        ha.next.next.next.next= hb.next.next .next.next.next = new ListNode(5);
        lc22.getIntersectionNode(ha,hb);
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode ha = headA,hb = headB;
        int i = 0;
        System.out.println(ha+"==="+hb);
        while (ha != hb){
            ha = ha == null ? headB:ha.next;
            hb = hb == null ? headA:hb.next;
            i++;
            System.out.println(ha+"==="+hb);
        }
        System.out.println(i);
        return ha;
    }
    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
        ListNode na = headA;
        while(na != null){
            ListNode nb = headB;
            while(nb != null){
                if(na == nb) return nb;
                nb = nb.next;
            }
            na = na.next;
        }
        return null;
    }
}
